package creational.singleton.lazyman;

import creational.singleton.Singleton;

/**
 * 懒汉式单例3,线程安全,锁范围小,性能较优
 * <br/>
 * 使用双重校验确保线程安全
 *
 * @author binbin
 * @since 2022/10/4 14:29
 */
public class LazyManSingleton3 implements Singleton {

    private static volatile LazyManSingleton3 instance;

    private static boolean init_flag = false;

    private static final Object LOCK = new Object();

    private LazyManSingleton3() {
        if (init_flag) {
            throw new RuntimeException("单例模式,不能做坏事哦");
        }
        init_flag = true;
    }

    public static LazyManSingleton3 getInstance() {
        if (instance == null) {
            synchronized (LOCK) {
                if (instance == null) {
                    instance = new LazyManSingleton3();
                }
            }
        }
        return instance;
    }
}
